/**
 * Created by asus on 2017/4/28.
 */
import http from '../../../common/httpUtil'
import * as types from './mutationTypes'

const actions = {
  querySysNews({commit}, {index = 1, size = 10} = {}) {
    commit(types.NEWS_QUERY_NEWS_BEGIN)
    let url = `sys/news?page=${index}&size=${size}`;
    return http.getRaw(url).then(result => {
      //之前的代码都没有对currentPage做修改，我们这里做,同时totalCount也在这处理
      let news = result.body.objs;
      let currentPage = result.body.page;
      let totalCount = result.body.totalNum;
      commit(types.NEWS_QUERY_NEWS_SUC, {news, currentPage, totalCount});
    })
  },
  deleteNews({commit}, params) {
    /*
    let data ={
      id: params.id
    };
   */
    let url = `sys/news/${params.id}`;
    return http.delete(url).then(() => {
      commit(types.NEWS_DELETE_SUC)
    });
  },
  setEditingNews({commit}, news) {
    commit(types.NEWS_SET_EDITING_NEWS, {news})
  },
  setPageModel({commit}, modelType) {
    commit(types.NEWS_SET_PAGE_MODEL, {modelType})
  },
  setSearchText({commit}, searchText) {
    commit(types.NEWS_SET_SEARCH_TEXT, {searchText})
  },
  searchSysNews({commit}, {searchText, index = 1, size = 10} = {}) {
    //TODO 到底是a还是s
    let url = `sys/news/search?keyword=${searchText}&page=${index}&size=${size}`;
    return http.getRaw(url).then(result => {
      let news = result.body.objs;
      let currentPage = result.body.page;
      let totalCount = result.body.totalNum;
      commit(types.NEWS_SEARCH_NEWS_SUC, {news, currentPage, totalCount});
    });
  },
  releaseNews({commit}, news) {
    let url = 'sys/news'
    return http.post(url, news).then(data => {
      commit(types.NEWS_RELEASE_SUC)
    })
  },
  updateNews({commit}, news) {
    let url = 'sys/news';
    return http.post(url, news).then(() => {
      commit(types.NEWS_UPDATE_SUC)
    });
  }
}
export default actions
